<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态按钮图标</title>
</head>

<body>
    <div class="box">
        <!--按钮-->
        <div class="header-action-button hamburger-menu" onclick="toggleActive()"></div>
    </div>
    <script>
        const idClass = document.querySelector(".box");
        const toggleActive = () => {
            console.log(idClass.classList);
            if (idClass.classList.contains('active')) {
                idClass.classList.remove('active');
            } else {
                idClass.classList.add('active');
            }
        };
    </script>
    <style>
        ::before,
        ::after {
            box-sizing: border-box;
        }



        .header-action-button {
            position: relative;
            width: 4rem;
            height: 4rem;
            margin-left: 1rem;
            cursor: pointer;
        }

        .header-action-button.hamburger-menu::before {
            content: "";
            position: absolute;
            top: -.9rem;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            width: 2rem;
            height: .2rem;
            border-top: .2rem solid currentColor;
            transition: .4s;
        }

        .header-action-button.hamburger-menu::after {
            content: "";
            position: absolute;
            /* top: -.9rem;*/
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            width: 2rem;
            height: .2rem;
            border-top: .2rem solid currentColor;
            transition: .4s;

            top: .9rem;
        }

        .box.active .header-action-button.hamburger-menu::before {
            -webkit-transform: rotate(45deg) translate(0.3rem, 0.2rem);
            transform: rotate(45deg) translate(0.3rem, 0.2rem);
        }

        .box.active .header-action-button.hamburger-menu::after {
            -webkit-transform: rotate(-45deg) translate(0.4rem, -0.3rem);
            transform: rotate(-45deg) translate(0.4rem, -0.3rem);
        }
    </style>

</body>

</html>